<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<mat-toolbar color="primary">
  <h2 translate>mqtt-client-credentials.add-client-credentials</h2>
  <span class="flex flex-1"></span>
  <div [tb-help]="'clientCredentials'"></div>
  <button mat-icon-button
    (click)="cancel()"
    type="button">
    <mat-icon class="material-icons">close</mat-icon>
  </button>
</mat-toolbar>
@if (isLoading$ | async) {
  <mat-progress-bar color="warn" mode="indeterminate">
  </mat-progress-bar>
}
<div mat-dialog-content>
  <mat-horizontal-stepper #addClientCredentialsWizardStepper
    [linear]="true"
    [labelPosition]="(stepperLabelPosition | async)"
    [orientation]="(stepperOrientation | async)"
    (selectionChange)="changeStep($event)">
    <ng-template matStepperIcon="edit">
      <mat-icon>check</mat-icon>
    </ng-template>
    <mat-step [stepControl]="clientCredentialsWizardFormGroup">
      <form [formGroup]="clientCredentialsWizardFormGroup" #credentialFormGroup="ngForm" style="padding-bottom: 16px;">
        <ng-template matStepLabel>{{ 'mqtt-client-credentials.client-credentials-details' | translate}}</ng-template>
        <fieldset [disabled]="isLoading$ | async">
          <mat-form-field class="flex flex-1">
            <mat-label translate>mqtt-client-credentials.name</mat-label>
            <input matInput formControlName="name" required>
            <mat-icon matSuffix
                      matTooltipClass="tb-error-tooltip"
                      matTooltip="{{ 'shared-subscription.name-required' | translate }}"
                      [class.!hidden]="!(clientCredentialsWizardFormGroup.get('name').hasError('required') && clientCredentialsWizardFormGroup.get('name').touched)"
                      class="tb-error">
              warning
            </mat-icon>
          </mat-form-field>
          <mat-form-field class="flex flex-1" subscriptSizing="dynamic">
            <mat-label translate>mqtt-client.client-type</mat-label>
            <mat-select required formControlName="clientType">
              @for (clientType of clientTypes; track clientType) {
                <mat-option [value]="clientType">
                  {{ clientTypeTranslationMap.get(clientType) | translate }}
                </mat-option>
              }
            </mat-select>
          </mat-form-field>
          @if (clientCredentialsWizardFormGroup.get('clientType').value === ClientType.APPLICATION) {
            <div class="tb-hint"
              [innerHtml]="('mqtt-client-credentials.hint-client-type-application' | translate) + ' ' + ('mqtt-client-credentials.hint-client-type-read-more' | translate)">
            </div>
          }
          @if (clientCredentialsWizardFormGroup.get('clientType').value === ClientType.DEVICE) {
            <div class="tb-hint"
              [innerHtml]="('mqtt-client-credentials.hint-client-type-device' | translate) + ' ' + ('mqtt-client-credentials.hint-client-type-read-more' | translate)">
            </div>
          }
          <mat-form-field class="mat-block">
            <mat-label translate>mqtt-client-credentials.type</mat-label>
            <mat-select formControlName="credentialsType">
              @for (credentialsType of CredentialsTypes; track credentialsType) {
                <mat-option [value]="credentialsType">
                  {{ credentialsTypeTranslationMap.get(credentialsType) | translate }}
                </mat-option>
              }
            </mat-select>
          </mat-form-field>
          <div formGroupName="additionalInfo" class="flex flex-col">
            <mat-form-field class="mat-block" subscriptSizing="dynamic">
              <mat-label translate>user.description</mat-label>
              <textarea matInput formControlName="description" rows="2"></textarea>
            </mat-form-field>
          </div>
        </fieldset>
      </form>
    </mat-step>
    <mat-step [stepControl]="authenticationFormGroup">
      <ng-template matStepLabel>{{ 'mqtt-client-credentials.authentication-authorization' | translate }}</ng-template>
      <form [formGroup]="authenticationFormGroup" #authFormGroup="ngForm" style="padding-bottom: 16px;">
        <div>
          @switch (clientCredentialsWizardFormGroup.get('credentialsType').value) {
            @case (CredentialsType.MQTT_BASIC) {
              <tb-mqtt-credentials-basic formControlName="credentialsValue">
              </tb-mqtt-credentials-basic>
            }
            @case (CredentialsType.X_509) {
              <tb-mqtt-credentials-ssl formControlName="credentialsValue"></tb-mqtt-credentials-ssl>
            }
            @case (CredentialsType.SCRAM) {
              <tb-mqtt-credentials-scram formControlName="credentialsValue"></tb-mqtt-credentials-scram>
            }
          }
        </div>
      </form>
    </mat-step>
  </mat-horizontal-stepper>
</div>
<div mat-dialog-actions class="tb-dialog-actions">
  <div class="dialog-actions-row">
    @if (selectedIndex > 0) {
      <button mat-stroked-button
        [disabled]="(isLoading$ | async)"
      (click)="previousStep()">{{ 'action.back' | translate }}</button>
    }
    <span class="flex flex-1"></span>
    @if (showNext) {
      <button mat-stroked-button
        color="primary"
        [disabled]="(isLoading$ | async)"
      (click)="nextStep()">{{ 'action.next-with-label' | translate:{label: (getFormLabel(this.selectedIndex+1) | translate)} }}</button>
    }
  </div>
  <mat-divider style="width: 100%"></mat-divider>
  <div class="dialog-actions-row">
    <button mat-button
      [disabled]="(isLoading$ | async)"
    (click)="cancel()">{{ 'action.cancel' | translate }}</button>
    <button mat-raised-button
      [disabled]="(isLoading$ | async) || credentialFormGroup?.invalid || authFormGroup?.invalid"
      color="primary"
    (click)="add()">{{ 'action.add' | translate }}</button>
  </div>
</div>
